﻿using GemStone.GemFire.Cache.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 自定义示例
{
    class Program
    {

        //https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2041452

        //F:\GemFire\Pivotal_GemFire_NativeClient_64bit_8214\bin\GemStone.GemFire.Cache.dll
        static void Main(string[] args)
        {
            //以前的示例
            using (CacheFactory cacheFactory = CacheFactory.CreateCacheFactory())
            {
                using (Cache cache = cacheFactory
                    //.AddServer("localhost", 40404)
                                       .AddServer("127.0.0.1", 40404)
                    // .SetPRSingleHopEnabled(true)
                                       .SetSubscriptionEnabled(true)
                                       .Create())
                {

                    RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);
                    //这个Region需要已经存在的
                    IRegion<String, String> region = regionFactory.Create<String, String>("regionA");
                    //region  在命令gfsh 中使用
                    var gegion = regionFactory.Create<string, byte[]>("exampleRegion");
                    var idn = gegion.Count;
                    gegion.Add(new KeyValuePair<string, byte[]>("示例", System.Text.ASCIIEncoding.ASCII.GetBytes("数据")) { });
                    gegion["2"] = new byte[] { 1, 2, 3, 4, 4, 4, 5, 7, 57, 5 };
                    //换字符串类型 ,提示无法将Byte转换为string
                    //var gegionstr = regionFactory.Create<string, string>("exampleRegion");
                    //idn = gegion.Count;
                    //string x = gegionstr["2"];
                    //string abc = gegionstr["示例"];

                    Console.WriteLine("开始测试");

                    System.Threading.Tasks.Task.Factory.StartNew(() => {
                        while (true)
                        {
                            增加100个key(gegion);
                        }
                    });

                    //System.Threading.Tasks.Task.Factory.StartNew(() =>
                    //{
                    //    var r1 = regionFactory.Create<string, byte[]>("r1");

                    //    while (true)
                    //    {
                    //        增加100个key(r1);
                    //    }
                    //});

                    //System.Threading.Tasks.Task.Factory.StartNew(() =>
                    //{
                    //    var r2 = regionFactory.Create<string, byte[]>("r2");
                    //    while (true)
                    //    {
                    //        增加100个key(r2);
                    //    }
                    //});
                    long per = 0;
                    DateTime dt = DateTime.Now;

                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        while (true)
                        {
                            long sp = kn - per;
                            per = kn;
                            double ss = (sp) / (DateTime.Now - dt).TotalSeconds;
                            dt = DateTime.Now;
                            System.Threading.Thread.Sleep(5000);
                            Console.WriteLine("速度：{0:F2}" , ss);

                        }
                    });

                   

                    //Using the generic IDictionary API or by using the .NET Region.Put
                    region["11"] = "strValue";

                    //
                    Console.WriteLine("创建Region");

                    Console.Read();



                    cache.Dispose();
                }

            }
        }

       static long kn = 0;

        static byte[] kb8 = new byte[1024 * 8];
        static byte[] kb4 = new byte[1024 * 4]; 
        static void 增加100个key(IRegion<string, byte[]> reg)
        {
            for (int i = 0; i < 100; i++)
            {
                reg[i.ToString()] = kb4;
                //reg[i.ToString()] = Guid.NewGuid().ToByteArray();
                System.Threading.Interlocked.Increment(ref kn);
            }
        }

        static void 删除100个key(IRegion<string, byte[]> reg)
        {
            for (int i = 0; i < 100; i++)
            {
                var key = i.ToString();
                //var ddd = reg[key];
                reg.Remove(key);
            }
        }


        void 以前的示例()
        {
            // 1. Create a cache
            CacheFactory cacheFactory = CacheFactory.CreateCacheFactory();
            // cacheFactory.AddServer("127.0.0.1", 40411);
            //cacheFactory.AddLocator("127.0.0.1", 40404);

            Cache cache = cacheFactory.Create();


            // 2. Create default region attributes using region factory
            RegionFactory regionFactory =
               cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);

            // 3. Create a region
            IRegion<int, string> region =
               regionFactory.Create<int, string>("exampleRegion");

            // 4. Put some entries
            region[111] = "Value1";
            region[123] = "123";

            // 5. Get the entries
            string result1 = region[111];
            string result2 = region[123];

            // 6. Close cache
            cache.Close();
        }
    }
}
